package net.wantongtech.xa.uploadbasicinfo.service;

import com.wtkj.vo.RandomGUID;
import com.wtkj.vo.dzzf.base.xxdl.InteractionOrgMsgReceive;
import com.wtkj.zwxt.sjhj.constant.BizCodeType;
import com.wtkj.zwxt.sjhj.constant.IssuerIdStatus;
import com.wtkj.zwxt.sjhj.constant.MessageStatus;
import com.wtkj.zwxt.sjhj.util.FileUtils;
import lombok.extern.slf4j.Slf4j;
import net.wantongtech.xa.uploadbasicinfo.util.SecondBaseTemplate;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;

/**
 * @author MaShibo
 * 2021/3/15 0015
 */
@Service
@Slf4j
public class LoadReceiveExpandMsgService {

    public static final String SOURCE_PATH = "C:\\localStorePath\\MUNICIPAL_OPERATOR";

    public static final String STORE_PATH = "D:\\downloadFilePath";

    @Resource
    private SecondBaseTemplate secondBaseTemplate;


    public void load() {
        File dir = new File(SOURCE_PATH);
        File[] jsonFiles = dir.listFiles((dir1, name) -> name.endsWith(".json"));
        if (ArrayUtils.isEmpty(jsonFiles)) {
            return;
        }

        for (File jsonFile : jsonFiles) {
            log.info("处理文件：{}", jsonFile.getName());
            InteractionOrgMsgReceive rcvMsg = new InteractionOrgMsgReceive();
            rcvMsg.setListID(new RandomGUID().toString());
            rcvMsg.setSenderId(IssuerIdStatus.SX_CLEAR);
            rcvMsg.setMsgSeq(1);
            rcvMsg.setBizCode(BizCodeType.ETCTS_NEW_AU_BIZCODE);
            rcvMsg.setCreateOn(new Date());
            String fileName = jsonFile.getName();
            String msgId = FileUtils.getIssuerAndMessageIdName(fileName, 2, true);
            rcvMsg.setConMsgID(Long.valueOf(msgId));
            rcvMsg.setState(Integer.parseInt(MessageStatus.STATE_ORIG));
            rcvMsg.setReceiverId(FileUtils.getIssuerAndMessageIdName(fileName, 1, true));
            rcvMsg.setBankOrClear(1);
            rcvMsg.setMessageListId(new RandomGUID().toString());
            String tollProvinceId = FileUtils.getIssuerAndMessageIdName(fileName, 0, true);
            rcvMsg.setTollProvinceId(tollProvinceId);
            rcvMsg.setStoreFlag(InteractionOrgMsgReceive.STORE_FLAG_DISK);
            rcvMsg.setStorePath(new File(STORE_PATH, jsonFile.getName()).getAbsolutePath());
            rcvMsg.setServiceID(tollProvinceId);
            secondBaseTemplate.createValueObject(rcvMsg);
        }
    }
}
